package com.cw.demo.utils;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
@Aspect//表示当前类是一个切面类
public class logger {
@Pointcut("execution(* com.cw.demo.service.impl.*.*(..))")
    private void init(){}
    @Before("init()")
    public void berorPrintLog(){
        System.out.println("前置通知Logger类中的beforePrintLog方法开始记录日志了。。。");
    }
    @After("init()")
    public  void afterPrintLog(){
        System.out.println("后置通知Logger类中的afterReturningPrintLog方法开始记录日志了。。。");
    }
    @Around("init()")
    public Object aroundPringLog(ProceedingJoinPoint pjp){
    Object object=null;
        try {
      String classname=pjp.getTarget().getClass().getName();
      String methodname=pjp.getSignature().getName();
            List<Object> list=null;
            Object[] args=pjp.getArgs();
            Date now=new Date();
//            for (Object ob:args) {
////           list.add(ob);
////
////            }
            object=pjp.proceed(args);
            Date end=new Date();
            long time=end.getTime()-now.getTime();
            System.out.println("时间"+time+"类名"+classname+"方法"+methodname);
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return object;
    }
}
